package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrdersService;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/submit")
    public R<Orders> submit(@RequestBody Orders orders, HttpSession session){
        User userLogin=(User) session.getAttribute("userLogin");
        orders.setUserId(userLogin.getId());
        Boolean flag=ordersService.submit(orders);
//        System.out.println(orders);
        return R.success(orders);
    }


    @GetMapping("/userPage")
    public R<Page> userPage(int page ,int pageSize,HttpSession session){
        User userLogin=(User) session.getAttribute("userLogin");
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据付款时间 倒序
        ordersLambdaQueryWrapper.orderByDesc(Orders::getCheckoutTime);
        //获取用户的订单列表
        ordersLambdaQueryWrapper.eq(Orders::getUserId,userLogin.getId());
        ordersService.page(ordersPage,ordersLambdaQueryWrapper);
        //获取分页订单列表
        List<Orders> ordersList=ordersPage.getRecords();
        //分页数据模型列表 用于增强 转换
        List<OrdersDto> ordersDtoList = getOrdersDtoByOrdes(ordersList);
        //将原分页数据 增强
        Page<OrdersDto> ordersDtoPage = new Page<>();
        BeanUtils.copyProperties(ordersPage,ordersDtoPage);
        ordersDtoPage.setRecords(ordersDtoList);
        return R.success(ordersDtoPage);
    }


    @GetMapping("page")
    public R<Page> page(int page, int pageSize, String number,
                        @DateTimeFormat(pattern="yy-MM-dd HH:mm:ss") Date beginTime,
                        @DateTimeFormat(pattern="yy-MM-dd HH:mm:ss") Date endTime){
        System.out.println(beginTime);
        System.out.println(endTime);
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.like(number != null,Orders::getNumber,number);
        ordersLambdaQueryWrapper.between(beginTime != null,Orders::getOrderTime,beginTime,endTime);
        //根据付款时间 倒序
        ordersLambdaQueryWrapper.orderByDesc(Orders::getCheckoutTime);
        ordersService.page(ordersPage,ordersLambdaQueryWrapper);
        //获取分页订单列表
        List<Orders> ordersList=ordersPage.getRecords();

        List<OrdersDto> ordersDtoList=getOrdersDtoByOrdes(ordersList);
        //将原分页数据 增强
        Page<OrdersDto> ordersDtoPage = new Page<>();
        BeanUtils.copyProperties(ordersPage,ordersDtoPage);
        ordersDtoPage.setRecords(ordersDtoList);
        return R.success(ordersDtoPage);
    }

    /**
     * 将分页数据进行增强
     * @param ordersList
     * @return
     */
    public List<OrdersDto> getOrdersDtoByOrdes(List<Orders> ordersList){

        //分页数据模型列表 用于增强 转换
        List<OrdersDto> ordersDtoList = new ArrayList<>();
        for (Orders orders : ordersList) {
            //基础属性赋值
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(orders,ordersDto);
            //根据订单id 获取订单明细表
            LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
            orderDetailLambdaQueryWrapper.eq(OrderDetail::getOrderId,orders.getId());
            List<OrderDetail> list=orderDetailService.list(orderDetailLambdaQueryWrapper);
            ordersDto.setOrderDetails(list);
            //封装完成 添加到列表中
            ordersDtoList.add(ordersDto);
        }

        return ordersDtoList;
    }

    @PutMapping
    public R<String> changeOrdersStatus(@RequestBody Orders orders){
        boolean flag=ordersService.updateById(orders);

        return flag ? R.success("修改成功!") :R.error("修改失败");
    }

    @PostMapping("/again")
    public R<String> again(@RequestBody Orders orders,HttpSession session){
        LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
        orderDetailLambdaQueryWrapper.eq(OrderDetail::getOrderId,orders.getId());
        List<OrderDetail> list=orderDetailService.list(orderDetailLambdaQueryWrapper);
        for (OrderDetail orderDetail : list) {
            User userLogin=(User) session.getAttribute("userLogin");
            ShoppingCart shoppingCart = new ShoppingCart();
            BeanUtils.copyProperties(orderDetail,shoppingCart);
            //将赋值多余的id 清空
            shoppingCart.setId(null);
            //设置 用户id
            shoppingCart.setUserId(userLogin.getId());
            shoppingCartService.save(shoppingCart);
        }

        return R.success("成功");
    }
}
